﻿#include "stdafx.h"
#if _QG_ENABLE_WIDGET
#include "qg/qgasv.h"
#include "qg/qgmtrl.h"
#include "qg/qgrdraw.h"
#include "qg/qgwidget.h"

//////////////////////////////////////////////////////////////////////////
// 버튼
KIMPL_SUBCLASS(qwButton, "WidgetButton");

qwButton::qwButton(qgWidgetType type, kint instmask)
: super_type(type, QGWI_FILL | QGWI_INPUT | QGWI_SUNKEN | instmask)
, _blend(tpl::Color(0.25f, 0.25f, 0.25f), tpl::Color(0.25f, 0.25f, 0.25f))
{
}

void qwButton::OnInitialize()
{
	super_type::OnInitialize();

	_textstr.Format("button#%d", _id);
}

bool qwButton::OnUpdate(float adv)
{
	super_type::OnUpdate(adv);

	_blend.Update(adv, _shed->GetBlendSpeed());

	return true;
}

void qwButton::OnDraw()
{
	if (CanFill())
		_shed->DrawFillRect(_client, GetResource(), _tex);

	if (HasBorder())
		_shed->DrawEmptyRect(_client, _color_border);

	if (HasText())
	{
		qgWidgetTextInfo info(_textinfo);

		if (HasSunken())
		{
			if (_status == QGWS_SELECT)
				info.anch.x += 1, info.anch.y += 1;
#if 0
			else if (_status == QGWS_OVER)
				info.anch.x -= 1, info.anch.y -= 1;
#endif
		}

		_shed->DrawAnchText(_font, _textstr.Data(), _client, info, 0);
	}

	qwControl::OnDraw();
}

void qwButton::OnChangeStatus(qgWidgetStatus status)
{
	_blend.Reset(tpl::Color(_res[status].color), tpl::Color(_blend.color[0]));
}

bool qwButton::OnMouseDown(qgSevMouseButton& e)
{
	if (e.button == QIM_LEFT)
		return false;

	return true;
}

bool qwButton::OnMouseUp(qgSevMouseButton& e)
{
	if (e.button == QIM_LEFT)
	{
		if (_status == QGWS_SELECT && TestPick(e.x, e.y, _parent != NULL))
		{
			// 커맨드
			if (_ecb_click)
				_ecb_click(this);
		}
	}

	return true;
}

#endif
